<ul class="navigation">
<li>
<?php if ($this->auth === false): ?>
    <div class="box-message" id="message" style="display: none;"></div>
    <div id="login-form"><?php echo $this->form_auth ?></div>
    <label from="quick_expire" style="display: block; margin: 5px 0;">
        <input type="checkbox" name="quick_expire" id="quick_expire" />
        Чужой компьютер
    </label>
    <div class="btn">
        <button class="primary" id='#login-btn' onclick="login(this); return false;">Войти</button>
    </div>
    <span id="progress_ajax" style="display: none; margin-top: 5px;" class="progress ajax-preloader-base"></span>
    <script>
        var progress = $('#progress_ajax'),
            message = $('#message'),
            lf = $('#login-form');
		// hack
		$('#pp-auth-form').append('<input type="submit" style="visibility: hidden;" value=""/>').parent().submit(function(){
			window.login($('#enter-login'));
			return false;
		});
        function login(el) {
            var login = $('#auth_login'),
                password = $('#auth_password'),
                quick_expire = $('#quick_expire').prop('checked');
                
            if (!login.val()) {
                login.focus();
                return;
            } else if (!password.val()) {
                password.focus();
                return;
            }
            message.hide();
            $(el).parent().hide();
            progress.show();            
            $.ajax({
                'type': 'post',
                'url': '/personal.page/login',
                'data': {
                    'auth_login': login.val(),
                    'auth_password': password.val(),
                    'quick_expire': quick_expire,
                    'form_hash': $('#form_hash').val()
                },
                'dataType': 'json',
                'success': function(response) {
                    if (typeof response.error != 'undefined') {
                        message.html(response.error).show();
                        lf.html(response.form);
						$(el).parent().show();
						// hack
						$('#pp-auth-form').append('<input type="submit" style="visibility: hidden;" value=""/>').parent().submit(function(){
							window.login($('#enter-login'));
							return false;
						});
                    } else if (typeof response.redirect != 'undefined') {
                        window.location = response.redirect;
                    }
                    progress.hide();
                },
				'error': function() {
					alert('При обработке запроса возникли ошибки');
					$(el).parent().show();
                    progress.hide();
				}
            });
        }
    </script>
<?php else: ?>
	<ul>
		<?php
			// find page for user
			$s = Model_Page::getInstance()->select()->from('pages', array('id'))
					->where('name = ?', $this->user->login);
			$r = Model_Page::getInstance()->fetchRow($s);
			if ($r):
		?>
			<li><a href="/page/<?php echo $this->user->login ?>">Моя страница</a></li>
		<?php endif; ?>
		<li>
			<a href="/personal.page/messages">Личные сообщения</a>
			<span id="new-incoming">
				<?php if ($this->incoming > 0): ?><b>+<?php echo $this->incoming ?></b><?php endif; ?>
			</span>
		</li>
		<?php
			$groups = $this->client->groups;
			$groupPage = '';
			foreach ($groups as $group) {
				if ($group->id == 1 || $group->id == 2 || $group->id == 3 || $group->id == 4) {
					$groupPage = '';
				} else {
					$groupPage = $group->name;
					break;
				}
			}
			if (!empty($groupPage)):
		?>
			<li><a href="/page/<?php echo $groupPage ?>">Страница моей группы</a></li>
		<?php endif; ?>
		<li class="separator"><hr style="margin: 0px;"></li>
		<li><a href="/personal.page/logout">Выход</a></li>
	</ul>
<script>
function checkNewIncoming() {
	var data = {};
    data.href = '/personal.page/messages';
	$.getJSON('/personal.page/check.new.incoming', data, function(r){
		if (r.count_incoming > 0) {
			$('#new-incoming').html('<b>+' + r.count_incoming + '</b>').show();
			if (typeof r.messages == 'object') {
				$.each(r.messages, function(){
					if (!$('.list-message>li', '#incoming').is('#' + this.id)) {
						addMessageList('incoming', this);
					}
				});
			}
		} else {
			$('#new-incoming').hide();
		}
	});
	setTimeout(function(){
		checkNewIncoming();
	}, 10000);
}

checkNewIncoming();
function addMessageList(_for, data) {
    _for = _for || 'posted';
    html = '<li id="' + data.id + '"';
    if (_for == 'incoming') {
        html+= ' class="not-read" onclick="read(this);"'
    }
    html+= '>';
    html+= '<div class="message-author">' + data.for_user + '</div>';
    html+= '<div class="message-content">' + data.message + '</div>';
    html+= '<div class="message-info">' + data.created;
    if (_for == 'incoming') {
        html+= '<div class="right"><a href="#" onclick="answer(' + data.for_user_id + '); return false;">Ответить</a></div>';
    }
    html+= '</div></div></li>';
    $('#' + _for).find('ul').prepend(html);
}
</script>
<?php endif; ?>
</li>
</ul>